// index.ts
// 获取应用实例
Component({
  data: {
    motto: 'Hello World',
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  methods: {
    onShareAppMessage() {
      return {
        title: '油烟监控',
        path: 'pages/index/index'
      }
    },
    phoneLogin(e) {
      // wx.request({
      //   url: `http://8.148.22.60:16060/app-api/member/auth/login`,
      //   method: 'POST',
      //   header: {
      //     // 'Authorization': 'Bearer 9e0c322457214a5f9c19227d30c075fa'
      //   },
      //   data: {
      //     "mobile": "13535132284",
      //     "password": "admin123"
      //   },
      //   success: (res) => {
      //     console.log(res)
      //     if (res?.data?.data?.accessToken) {
      //       wx.setStorageSync('token', res?.data?.data?.accessToken)
      //       wx.redirectTo({
      //         url: '../index/index',
      //         fail: e => {
      //           console.log(e)
      //         }
      //       })
      //     } else {
      //       wx.showToast({
      //         title: res?.data?.message,
      //         icon: 'none'
      //       })
      //     }
      //   }
      // })

      // return
      console.log('phoneLogin', e)
      const {
        code: mobileCode,
        encryptedData: mbEncryptedData,
        iv: mbIv
      } = e?.detail
      wx.login({
        success: (res) => {
          const {
            code
          } = res
          wx.getUserInfo({
            success: function (res) {
              console.log('userInfo', res)
              const {
                encryptedData,
                iv,
                rawData
              } = res
              wx.request({
                url: `http://8.148.22.60:16060/app-api/member/auth/weixin-mini-app-login`,
                method: 'POST',
                data: {
                  mbEncryptedData,
                  mbIv,
                  loginCode: code,
                  phoneCode: mobileCode,
                  encryptedData,
                  iv,
                  rawData,
                  "clientId": "wxmappSmoke", "clientSecret": "smokeSecret",
                  "state": "9b2ffbc1-7425-4155-9894-9d5c08541d62"
                },
                success: (res) => {
                  console.log(res)
                  if (res?.data?.data?.accessToken) {
                    wx.setStorageSync('token', res?.data?.data?.accessToken)
                    wx.redirectTo({
                      url: '../index/index',
                      fail: e => {
                        console.log(e)
                      }
                    })
                  } else {
                    wx.showToast({
                      title: res?.data?.message,
                      icon: 'none'
                    })
                  }
                }
              })
            }
          })
        },
      })
    },
    login() {
      wx.request({
        url: `https://yyapi.lvseguanjia.cn/api-auth/oauth/token?grant_type=password&scope=all&clientType=wxmapp&client_id=wxmappSmoke&client_secret=smokeSecret&username=admin&password=123456`,
        method: 'POST',
        data: {
          grant_type: 'password',
          scope: 'all',
          clientType: 'wxmapp',
          client_id: 'wxmappSmoke',
          client_secret: 'smokeSecret',
          username: 'admin',
          password: '123456',
        },
        success: (res: any) => {
          console.log('login', res)
          wx.setStorageSync('token', res.data.access_token)
          wx.redirectTo({
            url: '../detail/index'
          })
        }
      })


    },
    // 事件处理函数
    bindViewTap() {
      wx.navigateTo({
        url: '../logs/logs',
      })
    },
    getUserProfile() {
      // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
      wx.getUserProfile({
        desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log(res)

        }
      })
    },
  },

})